package com.yfp.group.service.impl;

import com.yfp.group.domain.EnterpriseOrderCoupon;
import com.yfp.group.domain.EnterpriseOrderCouponInfo;
import com.yfp.group.mapper.EnterpriseOrderMapper;
import com.yfp.group.mapper.WorkOrderMapper;
import com.yfp.group.service.EnterpriseOrderCouponService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description: 券码管理
 *
 * @CreateDate: 2024/8/15 14:42
 * @UpdateUser:
 * @UpdateDate: 2024/8/15 14:42
 * @UpdateRemark: 暂无修改
 * @Version: 1.0
 */
@Service
public class EnterpriseOrderCouponServiceImpl implements EnterpriseOrderCouponService {
    @Resource
    private EnterpriseOrderMapper enterpriseOrderMapper;
    @Resource
    private WorkOrderMapper workOrderMapper;


    /**
     * 券码管理
     * @param enterpriseOrderCoupon
     * @return
     */
    @Override
    public List<EnterpriseOrderCoupon> findEnterpriseOrderCoupon(EnterpriseOrderCoupon enterpriseOrderCoupon) {
        return enterpriseOrderMapper.findEnterpriseOrderCoupon(enterpriseOrderCoupon);
    }

    /**
     * 券码管理详情
     */
    @Override
    public List<EnterpriseOrderCouponInfo> findEnterpriseOrderCouponInfo(EnterpriseOrderCouponInfo enterpriseOrderCouponInfo) {
        List<EnterpriseOrderCouponInfo> list = enterpriseOrderMapper.findEnterpriseOrderCouponInfo(enterpriseOrderCouponInfo);
        return list;
    }

    /**
     * 数据补全
     * @param list
     * @return
     */
    @Override
    public List<EnterpriseOrderCouponInfo> orderCouponInfoComplement(List<EnterpriseOrderCouponInfo> list) {

        if (!list.isEmpty()) {
            HashMap<String, EnterpriseOrderCouponInfo> map = new HashMap<>();
            for (EnterpriseOrderCouponInfo orderCouponInfo : list) {
                if (map.containsKey(orderCouponInfo.getOrderComboId())) {
                    EnterpriseOrderCouponInfo info = map.get(orderCouponInfo.getOrderComboId());
                    String comboName = info.getComboName();
                    String comboName1 = orderCouponInfo.getComboName();
                    Integer comboNum = info.getComboNum();
                    Integer comboNum1 = orderCouponInfo.getComboNum();
                    info.setComboPrice(info.getComboPrice().add(orderCouponInfo.getComboPrice().multiply(new BigDecimal(orderCouponInfo.getComboNum()))));
                    // 套餐名拼接
                    info.setComboName(comboName + "、" + comboName1);
                    // 套参数相加
                    info.setComboNum(comboNum + comboNum1);
                } else {
                    orderCouponInfo.setComboPrice(orderCouponInfo.getComboPrice().multiply(new BigDecimal(orderCouponInfo.getComboNum())));
                    map.put(orderCouponInfo.getOrderComboId(), orderCouponInfo);
                }
            }

            List<EnterpriseOrderCouponInfo> infos = new ArrayList<>(map.values());
            List<EnterpriseOrderCouponInfo> times = null;
            List<String> orderSns = infos.stream().filter(info -> info.getStatus().equals("Finish")).map(info -> info.getOrderComboId()).toList();
            if (!orderSns.isEmpty()) {
                times = workOrderMapper.getExitCloseTime(orderSns);
            }

            if (times != null && times.size() > 0) {
                Map<String, EnterpriseOrderCouponInfo> infoMap = times.stream().collect(Collectors.toMap(EnterpriseOrderCouponInfo::getOrderComboId, Function.identity()));
                for (String orderId : infoMap.keySet()) {
                    if (map.containsKey(orderId)) {
                        EnterpriseOrderCouponInfo info = map.get(orderId);
                        EnterpriseOrderCouponInfo enterpriseOrderCouponInfo1 = infoMap.get(orderId);
                        info.setFinishTime(enterpriseOrderCouponInfo1.getFinishTime());
                    }
                }
            }

            List<EnterpriseOrderCouponInfo> couponInfos = new ArrayList<>(map.values()).stream().sorted(Comparator.comparing(EnterpriseOrderCouponInfo::getRemark)).collect(Collectors.toList());
            return couponInfos;
        }

        return list;
    }
}
